const app = getApp();

Component({
  properties: {
    dynamics: Array
  },

  data: {},

  lifetimes: {},

  methods: {
    // 点赞动态触发此事件
    likeThis(e) {
      let dataset = e.currentTarget.dataset;
      let index = dataset.index;
      let data = { did: dataset.id };
      app.action
        .likeDynamic(data)
        .then(res => {
          console.log(res);
          if (res.code === 1000) {
            this.setData({
              [`dynamics[${index}].islike`]: 1,
              [`dynamics[${index}].like_num`]:
                this.data.dynamics[index].like_num + 1
            });
          } else if (res.code === 1003 || res.code === 1005) {
            app.setToast({ title: "登录后才能点赞哦" });
          } else if (res.code === 2000) {
            app.setToast({ title: "你已经点过赞啦" });
          } else {
            app.setToast({ title: "点赞失败啦" });
          }
        })
        .catch(res => {
          console.log(res);
          app.setToast({ title: "点赞失败啦" });
        });
    }
  }
});
